/*
 Copyright (C) 2012  Nikos Siatras
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
package ncore_tcp_port_forward.Core;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;
import ncore_tcp_port_forward.Core.AccessList.AccessList;

/**
 *
 * @author Nikos Siatras
 */
public class TCPForward implements Serializable
{

    private String fName, fLocalIPAddress, fRemoteIPAddress;
    private int fLocalPort, fRemotePort;
    private boolean fIsStarted;
    private int fThreadPoolCorePoolSize, fThreadPoolMaximumPoolSize;
    private int fMaximumConnections = 10;
    private AccessList fMyAccessList;
    transient private TCPServer fMyServer;

    public TCPForward(String name, String localIPAddress, int localPort, String remoteIPAddress, int remotePort, int corePoolSize, int maxPoolSize, int maximumConnections) throws UnknownHostException
    {
        fName = name;
        fLocalIPAddress = localIPAddress;
        fLocalPort = localPort;
        fRemoteIPAddress = remoteIPAddress;
        fRemotePort = remotePort;

        fMaximumConnections = maximumConnections;

        fThreadPoolCorePoolSize = corePoolSize;
        fThreadPoolMaximumPoolSize = maxPoolSize;
        
        fMyAccessList = new AccessList();
    }

    /**
     * Start the TCP Forward
     * @throws IOException
     * @throws Exception 
     */
    public void Start() throws IOException, Exception
    {
        Stop();
        fMyServer = new TCPServer(this, fName, "", fThreadPoolCorePoolSize, fThreadPoolMaximumPoolSize);
        fMyServer.AddListener(fName, InetAddress.getByName(fLocalIPAddress), fLocalPort, fMaximumConnections, 8192, 5000, 1000);

        fMyServer.Start();
        fMyAccessList.StartUpdatingIPs();

        fIsStarted = true;
    }

    /**
     * Stop the TCP forward
     */
    public void Stop()
    {
        if (fMyServer != null)
        {
            fMyServer.Stop();
            fMyServer.Dispose();
            fMyServer = null;
        }
        fIsStarted = false;
    }
    
    /**
     * Returns the access list of the TCP Forward
     * @return 
     */
    public AccessList getAccessList()
    {
        return fMyAccessList;
    }
    

    public String getName()
    {
        return fName;
    }

    public String getLocalIPAddress()
    {
        return fLocalIPAddress;
    }

    public int getLocalPort()
    {
        return fLocalPort;
    }

    public String getRemoteIPAddress()
    {
        return fRemoteIPAddress;
    }

    public int getRemotePort()
    {
        return fRemotePort;
    }

    public int getCurrentConnectionsNumber()
    {
        if (fMyServer != null)
        {
            return fMyServer.getCurrentConnectionsNumber();
        }
        else
        {
            return 0;
        }
    }

    public int getBytesIn()
    {
        if (fMyServer != null)
        {
            return fMyServer.getBytesIn();
        }
        else
        {
            return 0;
        }
    }

    public int getBytesOut()
    {
        if (fMyServer != null)
        {
            return fMyServer.getBytesOut();
        }
        else
        {
            return 0;
        }
    }

    public int getMaxConnections()
    {
        return fMaximumConnections;
    }

    public boolean isStarted()
    {
        return fIsStarted;
    }

    public int getCorePoolSize()
    {
        return fThreadPoolCorePoolSize;
    }

    public int getMaxPoolSize()
    {
        return fThreadPoolMaximumPoolSize;
    }
}
